package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.bean.ProjectRepayPlan;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;

public class DealPlanAdapter extends BaseAdapter {

    private ArrayList<ProjectRepayPlan> list;
    private LayoutInflater minflater;

    private ArrayList<Integer> showHeItemPosition = null; //集合用来保存需要显示头布局的列表位置

    private LayoutParams params;
    private int dpToPx2;

    public DealPlanAdapter(Activity activity, ArrayList<ProjectRepayPlan> list) {
        super();
        this.list = list;
        minflater = LayoutInflater.from(activity);
        showHeItemPosition = new ArrayList<Integer>();

        int dpToPx1 = (int) ((int) activity.getResources().getDisplayMetrics().density
                             * (float) 0.5 + 0.5f);//dp转像素0.5
        dpToPx2 = (int) ((int) activity.getResources().getDisplayMetrics().density * (float) 15 + 0.5f); //dp转像素15
        params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dpToPx1);

        prepareData(); //遍历数据，看哪些是分组月

    }

    private void prepareData() {
        if (!ListUtils.isEmpty(list)) {
            showHeItemPosition.removeAll(showHeItemPosition);
            String tempSpecialTime = null;
            showHeItemPosition.add(0);
            for (int i = 0; i < list.size(); i++) {
                String specialTime = null;
                if (list.get(i).getStatus() == (41 | 51 | 62)) {
                    specialTime = list.get(i).getRealTime().substring(0, 6);
                } else {
                    specialTime = list.get(i).getOughtedTime().substring(0, 6);
                }
                if (!specialTime.equals(tempSpecialTime)) {
                    tempSpecialTime = specialTime;
                    showHeItemPosition.add(i);
                }
            }
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MViewholder mViewHolder = null;
        final int _position = position;
        if (convertView == null) {
            mViewHolder = new MViewholder();
            convertView = minflater.inflate(R.layout.repayment_plan_item, null);
            mViewHolder.plan_mouth = (TextView) convertView.findViewById(R.id.plan_mouth);
            mViewHolder.ll_1 = (LinearLayout) convertView.findViewById(R.id.ll_1); //头部是否显示
            mViewHolder.view1 = (View) convertView.findViewById(R.id.view1); //线条是否顶头

            mViewHolder.repayment_item_time = (TextView) convertView
                .findViewById(R.id.repayment_item_time);
            mViewHolder.project_name = (TextView) convertView.findViewById(R.id.project_name);
            mViewHolder.project_code = (TextView) convertView.findViewById(R.id.project_code);
            mViewHolder.money_text = (TextView) convertView.findViewById(R.id.money_text);
            mViewHolder.repayment_type = (TextView) convertView.findViewById(R.id.repayment_type);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MViewholder) convertView.getTag();
        }
        ProjectRepayPlan inverst = list.get(_position);
        if (inverst.getStatus() == (41 | 51 | 62)) {
            mViewHolder.repayment_item_time.setText(StringUtils.formatString(inverst.getRealTime()
                .substring(0, 8)));
            mViewHolder.money_text.setText(ArithUtils.coverMoneyComma((Double.parseDouble(inverst.getRealCapital()) + Double
                    .parseDouble(inverst.getRealInterest())) + ""));
            mViewHolder.plan_mouth.setText(StringUtils.formatString(inverst.getRealTime().substring(0,
                6)));
            if ("0.00".equals(inverst.getRealCapital())) {
                mViewHolder.repayment_type.setText("利息");
            } else if ("0.00".equals(inverst.getRealInterest())) {
                mViewHolder.repayment_type.setText("本金");
            } else {
                mViewHolder.repayment_type.setText("本金+利息");
            }
        } else {
            mViewHolder.repayment_item_time.setText(StringUtils.formatString(inverst
                .getOughtedTime().substring(0, 8)));
            mViewHolder.money_text.setText(ArithUtils.coverMoneyComma((Double.parseDouble(inverst.getCapital()) + Double
                    .parseDouble(inverst.getInterest())) + ""));
            mViewHolder.plan_mouth.setText(StringUtils.formatString(inverst.getOughtedTime().substring(0,
                6)));
            if ("0.00".equals(inverst.getCapital())) {
                mViewHolder.repayment_type.setText("利息");
            } else if ("0.00".equals(inverst.getInterest())) {
                mViewHolder.repayment_type.setText("本金");
            } else {
                mViewHolder.repayment_type.setText("本金+利息");
            }
        }
        mViewHolder.project_name.setText(inverst.getProjectName());
        mViewHolder.project_code.setText(inverst.getProjectNid());

        if (showHeItemPosition.contains(position)) {
            mViewHolder.ll_1.setVisibility(View.VISIBLE);
            params.setMargins(0, 0, 0, 0);
            mViewHolder.view1.setLayoutParams(params);
            mViewHolder.view1.setVisibility(View.VISIBLE);
        } else {
            mViewHolder.ll_1.setVisibility(View.GONE);
            params.setMargins(dpToPx2, 0, 0, 0);
            mViewHolder.view1.setLayoutParams(params);
            mViewHolder.view1.setVisibility(View.VISIBLE);
        }
        if (position == 0) {
            mViewHolder.view1.setVisibility(View.GONE);
        }
        return convertView;
    }

    public void resetData(List<ProjectRepayPlan> repayments) {
        if (!repayments.isEmpty()) {
            list.clear();
            list.addAll(repayments);
            prepareData();
            notifyDataSetChanged();
        }

    }

    public void addData(List<ProjectRepayPlan> repayments) {
        if (!repayments.isEmpty()) {
            list.addAll(repayments);
            prepareData();
            notifyDataSetChanged();
        }
    }

    class MViewholder {
        public TextView repayment_type;
        public TextView money_text;
        public TextView project_code;
        public TextView project_name;
        public TextView repayment_item_time;
        public TextView plan_mouth;
        public TextView message_time;
        public LinearLayout ll_1;
        public View view1;
    }

}
